;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.BorrowStatistical", [])
        .controller("BorrowStatisticalCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {

            $scope.mainTbConf = {
                scrollWidth: 350,
                tableCol: [
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 100,
                        isShow: true,
                    },
                    {
                        name: "服务人员名称",
                        dataField: "userName",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "总额",
                        dataField: "summaryAmount",
                        width: 100,
                        isShow: true,
                    },
                ]
            };
            //表格头信息
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        name: "服务人员名称",
                        dataField: "userName",
                        width: 170,
                        isShow: true,
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 170,
                        isShow: true,
                        clickFn: "viewMaterialDetail",
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 120,
                        isShow: true,
                    },
                    {
                        name: "单位",
                        dataField: "unit",
                        width: 120,
                        isShow: true,
                    },
                    {
                        name: "单价",
                        dataField: "price",
                        width: 140,
                        isShow: true,
                    },
                    {
                        name: "借用数量",
                        dataField: "quantity",
                        width: 140,
                        isShow: true,
                    },
                    {
                        name: "总计",
                        dataField: "subtotal",
                        width: 140,
                        isShow: true,
                    }
                ]
            };

            $scope.viewTbConf = {
                scrollWidth: 1500,
                tableCol: [
                    {
                        name: "借用单号",
                        width: 180,
                        dataField: "borrowOutNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "借用数量",
                        width: 100,
                        dataField: "quantity",
                        input: true,
                        inputType: "txt"
                    },
                    {
                        name: "单价",
                        width: 100,
                        dataField: "price",
                        input: true,
                        inputType: "price"
                    },
                    {
                        name: "小计",
                        width: 100,
                        dataField: "subtotalAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "成本单价",
                        width: 100,
                        dataField: "costPrice",
                        input: true,
                        inputType: "price"
                    },
                    {
                        name: "成本小计",
                        width: 100,
                        dataField: "costSubtotal",
                        input: true,
                        inputType: "price"
                    },
                    {
                        name: "实际出库数量",
                        width: 120,
                        dataField: "outQuantity",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "转销售数量",
                        dataField: "resaleQty",
                        width: 100,
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已退回数量",
                        dataField: "rollBackQty",
                        width: 100,
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    }
                ]
            };

            $scope.activeFlag = [true, false];

            //高级搜索的展示配置
            $scope.toolbarConfig = {};

            //主列表搜索条件
            $scope.mainCondition = {};

            //搜索条件
            $scope.condition = {};

            $scope.mainPageInfo = {
                pageSize: 10,            //每页显示几条
                curPage: 1,              //当前页
                totalPages: 0,           //总共几页
                totalRows: 0             //总共几条数据
            };

            $scope.pageInfo = {
                pageSize: 10,            //每页显示几条
                curPage: 1,              //当前页
                totalPages: 0,           //总共几页
                totalRows: 0             //总共几条数据
            };

            // 详情页的分页信息
            $scope.detailPageInfo = {
                pageSize: 10,            //每页显示几条
                curPage: 1,              //当前页
                totalPages: 0,           //总共几页
                totalRows: 0             //总共几条数据
            };

            $scope.markInfo = {
                allCheckedService: false
            };

            $scope.serviceObj = {};

            $scope.exportParam = {
                mainUrl: ybHttp.RequestUrl("device", "/BorrowUsing/exportAllTotalAmountList"),
                sectionUrl: ybHttp.RequestUrl("device", "/BorrowUsing/export"),
                sectionParam: {ids: []},
                detailParam: {},
                allUrl: ybHttp.RequestUrl("device", "/BorrowUsing/exportAll"),
                allDetailUrl: ybHttp.RequestUrl("device", "/BorrowUsing/exportAllDetail")
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
            };

            function getBillIds() {
                var ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        ids.push(item.id);
                    }
                });
                $scope.exportParam.sectionParam.ids = ids;
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewMaterialDetail":
                        viewMaterialDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.viewDetail = function (item) {
                $scope.condition.userId = item.userId;
                $scope.dataList = [];
                $scope.getBorrowList();
                $scope.changeTab(1);
            };

            // 查看零件借用详情
            function viewMaterialDetail (item) {
                $scope.serviceObj.staffInfo = item;
                $scope.exportParam.detailParam.id = item.id;
                $scope.getBorrowUsingDetail().then(function (data) {
                    data && $scope.changeTab(2);
                });
            }

            $scope.getBorrowUsingDetail = function () {
                var url = '/BorrowUsing/detail';
                var param = {
                    filters: {
                        id: $scope.serviceObj.staffInfo.id
                    },
                    pageSize: $scope.detailPageInfo.pageSize,
                    pageNum: $scope.detailPageInfo.curPage
                };
                return ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return false;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                    });
                    $scope.serviceObj.materialList = res.value.list;
                    $scope.detailPageInfo.totalRows = res.value.total;
                    $scope.detailPageInfo.totalPages = res.value.pages;
                    return true;
                })
            };

            $scope.mainDataList = [];

            //服务借用类别数据
            $scope.dataList = [];

            // 暴露在外的搜索方法
            $scope.search = function () {
                getDataList();
            };

            //切换高级搜索条件展示 和 隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            //重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
            };

            //获取服务借用列表
            $scope.getBorrowList = function () {
                var service = "device";
                var reqUrl = "/BorrowUsing/list";
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition).forEach(function (key) {
                    if (!$scope.condition[key]) delete $scope.condition[key];
                });
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: $scope.condition
                };
                return ybHttp.Post(service, reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return false;
                    }
                    $scope.markInfo.allCheckedService = false;
                    angular.forEach(res.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                    return true;
                })
            };

            function getDataList () {
                var reqUrl = "/BorrowUsing/totalAmountList ";
                var param = {
                    pageSize: $scope.mainPageInfo.pageSize,
                    pageNum: $scope.mainPageInfo.curPage,
                    filters: $scope.mainCondition
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.markInfo.allCheckedService = false;
                    angular.forEach(res.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.mainPageInfo.totalRows = res.value.total;
                    $scope.mainPageInfo.totalPages = res.value.pages;
                    $scope.mainDataList = res.value.list;
                })
            }

            (function () {
                getDataList();
            })();

        }])
    ;
})(window, window.angular);